﻿using Blm.PgsqlDriver.Helpers;
using Blm.Utils.Extensions;
using Blm.Utils.Utility;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Net.Security;
using System.Threading.Tasks;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Utility;

namespace VisionCloud.Service.Ms
{
    /// <summary>
    /// 计算服务
    /// </summary>
    public class ComputeService
    {
        private static readonly string server = ConfigHelper.GetValue<string>("WebUrl");

        static ComputeService()
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((s, c, c2, c3) => { return true; });
        }

        /// <summary>
        /// 局域网节点计算点云
        /// </summary>
        /// <returns></returns>
        public static async Task<string> ComputeDotCloud(ComputeDotCloud taskComputeDotCloud)
        {
            string msg = string.Empty;
            var api = $"{server}/admin/task/start";
            var header = new Dictionary<string, string> { { "Authorization", "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ3dXFpb25nbGVpIiwic2NvcGUiOlsiYWxsIl0sImlkIjoxMCwiZXhwIjoxNjQyMDcwNjY2LCJhdXRob3JpdGllcyI6WyI4X-a1i-mHj-acjeWKoeeuoeeQhuWRmCJdLCJqdGkiOiI2N2YwNGRiMi1iNzNiLTQ3MmMtODZmZS1lZmViYzc4ODJjZWMiLCJjbGllbnRfaWQiOiJhZG1pbi1hcHAifQ.bxyG3jzrU-0-ViTbTJQoP5CdL22HFI-0mMA5wTMGxXFfNI5gtXEzgxPZd6urHvfBtWb7B-tenPnM1bS2XABB-ndXbOyGTvDbmPd1Y-PQ4WlM47uQ_nXHhGv7jVPX_VhqQwxsaSy_sWWdZbv3FYFQoFVp3yNc_rYQJsWqL9TokBE" } };
            TaskComputeDotCloudParam taskComputeDotCloudParam = new TaskComputeDotCloudParam();
            taskComputeDotCloudParam.batchId = "A01";
            taskComputeDotCloudParam.fileId = taskComputeDotCloud.FileId;
            taskComputeDotCloudParam.taskId = taskComputeDotCloud.TaskId;
            taskComputeDotCloudParam.pointIndex = taskComputeDotCloud.Site.ToInt();
            taskComputeDotCloudParam.dotCloudFileName = taskComputeDotCloud.DotCloudFilePath;
            using (var response = await HttpUtility.PostAsync(api, JsonConvert.SerializeObject(taskComputeDotCloudParam), 60, header))
            {
                if (response == null)
                {
                    msg = $"{api}远程服务器没响应!";
                    return msg;
                }
                if (response.IsSuccessStatusCode)
                {
                    var responseStr = await response.Content.ReadAsStringAsync();
                    var apiResult = JsonConvert.DeserializeObject<ComputeResult>(responseStr);
                    if (apiResult.Code == 200 && apiResult.Data.ToInt() > 0)
                    {

                        string sql = $"update \"MsTaskDotCloud\" set \"ComputeStatus\"='20' where \"FileId\"='{taskComputeDotCloud.FileId}';";
                        int i = PgSqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                        if (i > 0)
                        {
                            msg = string.Empty;
                        }
                    }
                    else
                    {
                        msg = $"result code: {responseStr}[{response.StatusCode}]";
                        return msg;
                    }
                }
                else
                {
                    var responseStr = await response.Content.ReadAsStringAsync();
                    msg = $"result code: {responseStr}[{response.StatusCode}]";
                    return msg;
                }
            }

            return msg;
        }
    }
}
